import { message } from "antd";
import { getCreateDraftList, getContentDetailsOther, getDetailesStatisticalData, pushDrafcheck, releaseDrafcheck, getPushedDraftData, getSiteList } from "../services/services";
export default {
  namespace: "draftlist",

  state: {
    tableData: [],//表格数据
    total: 0,//总数
    detail: {},//文章详情
    statistiData: [],//详情统计数据
    pushedTableData: [],//详情推送数据
    siteList: null
  },
  // 订阅
  subscriptions: {},
  // 同步
  reducers: {
    setTableData(state, action) {
      return { ...state, tableData: action.tableData };
    },
    setTotal(state, action) {
      return { ...state, total: action.total };
    },
    setDetail(state, action) {
      return { ...state, detail: action.detail };
    },
    setStatistiData(state, action) {
      return { ...state, statistiData: action.statistiData };
    },
    setPushedTableData(state, action) {
      return { ...state, pushedTableData: action.pushedTableData };
    },
    modules(state, payload) {
      return {
        ...state,
        ...payload.payload,
      };
    },
  },
  // 异步
  effects: {
    /**
     * 获取稿件列表
     */
    *getCreateDraftList({ payload }, { call, put, select }) {
      const res = yield call(getCreateDraftList, payload);
      if (res.code === 200) {
        yield put({ type: "setTableData", tableData: res.data.data });
        yield put({ type: "setTotal", total: res.data.total });
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
    /**
     * 获取文章详情
     */
    *getContentDetailsOther({ payload }, { call, put, select }) {
      const res = yield call(getContentDetailsOther, payload);
      if (res.code === 200) {
        yield put({ type: "setDetail", detail: res.data });
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
    /**
     * 文章详情统计数据
     */
    *getDetailesStatisticalData({ payload }, { call, put, select }) {
      const res = yield call(getDetailesStatisticalData, payload);
      if (res.code === 200) {
        let sdata = [];
        let arr = ["阅读量", "评论量", "点赞量", "分享量"];
        let arr1 = ["scan_count", "comment_count", "like_count", "share_count"];
        for (let i = 0; i < 4; i++) {
          let obj = {};
          obj["x"] = arr[i];
          obj["y"] = res.data[arr1[i]];
          sdata.push(obj);
        }
        yield put({ type: "setStatistiData", statistiData: sdata });
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
    /**
    * 通过审核
    */
    *pushDrafcheck({ payload }, { call, put, select }) {
      const res = yield call(pushDrafcheck, payload);
      if (res.code === 200) {
        message.success(res.msg)
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
    /**
    * 详情表格
    */
    *getPushedDraftData({ payload }, { call, put, select }) {
      const res = yield call(getPushedDraftData, payload);
      if (res.code === 200) {
        yield put({ type: "setPushedTableData", pushedTableData: res.data.data });
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
    /**
    * 发稿
    */
    *releaseDrafcheck({ payload }, { call, put, select }) {
      const res = yield call(releaseDrafcheck, payload);
      if (res.code === 200) {
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
    /**
    * 获取站点列表
    */
    *getSiteList({ payload }, { call, put, select }) {
      const res = yield call(getSiteList, payload);
      if (res.code === 200) {
        yield put({
          type: 'modules',
          payload: {
            siteList: res.data.data,
          }
        });
      } else {
        message.error(res.msg);
      }
    }
  }
};
